package cn.g31010.sip.component
{

import mx.utils.Delegate;
import mx.controls.Button;
	import mx.controls.TextInput;
	import flash.display.Sprite;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.events.NetStatusEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	//import flash.net.ObjectEncoding;

	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	public class MyCamera
	{
		public function MyCamera()
		{
		}

//创建一个基本的网络连接对象
var nc:NetConnection = new NetConnection();
//连接到本地的Red5服务器
nc.connect("rtmp://localhost/online");
//将已和red5服务器建立连接的nc传递给NetStream
var ns:NetStream = new NetStream(nc);
//从摄像头和麦克风取得信息
var cam:Camera = Camera.get();
var mic:Microphone = Microphone.get();
//初始化摄像头和麦克风，调用函数
setupCameraMic();
//保存上次录制的内容，并进行清空
var lastVideoName:String = "";
//监听开始录制和停止录制两个按键的点击动作并进行相应的处理
playButton.addEventListener("click", Delegate.create(this, playClick));
stopButton.addEventListener("click", Delegate.create(this, stopClick));
//定义开始录制的playclick函数
function playClick():Void
{
//此flv文件是保存在 webapps/oflaDemo/streams/文件夹
//将摄像头和麦克风赋给ns
ns.attachVideo(cam);
ns.attachAudio(mic);
//将摄像头信息赋给视频显示窗口，这样我们可以即时看到信息
videoContainer.attachVideo(cam);
// 此处是定义存储名字的，用随机数目获得一个唯一的文件名
lastVideoName = "red5RecordDemo_" + random(getTimer());
ns.publish(lastVideoName, "record");
//锁定开始按键使其失效
playButton.enabled = false;
//锁定停止按键使其生效
stopButton.enabled = true;
}
function stopClick():Void
{
//关闭ns与red5的连接
ns.close();
//并清空视频显示窗口
videoContainer.attachVideo(null);
videoContainer.clear();
//锁定开始按键使其生效
playButton.enabled = true;
//锁定停止按键使其失效
stopButton.enabled = false;
//播放已经录制的视频
playLastVideo();
}

function playLastVideo():Void
{
// 传递录制的视频信息给视屏显示窗口
videoContainer.attachVideo(ns);
//回访已经上次录制的视频片段
ns.play(lastVideoName);
}
function setupCameraMic():Void
{
//启动摄像头
cam = Camera.get();
//设置尺寸
cam.setMode(320, 240, 30);
//设置录制质量
cam.setQuality(0,70);
//启动麦克风
mic = Microphone.get();
mic.setRate(44);
}
	}
}